package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.LegalCasesQueryDTO;
import com.jeff.tianti.cms.dto.TeachQueryDTO;
import com.jeff.tianti.cms.dto.font.LegalCaseFontQueryDTO;
import com.jeff.tianti.cms.entity.LegalCases;
import com.jeff.tianti.cms.entity.Teach;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ColumnInfoDaoImpl类
 * @date 2017-03-16
 */

public class LegalCasesDaoImpl extends CustomBaseSqlDaoImpl implements LegalCasesDaoCustom {

    public PageModel<LegalCases> queryLegalCasesPage(LegalCasesQueryDTO legalCasesQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from LegalCases t where 1=1");
        if (legalCasesQueryDTO != null) {
            if (StringUtils.isNotBlank(legalCasesQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + legalCasesQueryDTO.getName() + "%");
            }
            if (StringUtils.isNotBlank(legalCasesQueryDTO.getMediaType())) {
                hql.append(" and t.mediaType like :mediaType ");
                map.put("mediaType", legalCasesQueryDTO.getMediaType());
            }

            if (StringUtils.isNotBlank(legalCasesQueryDTO.getIndustryType())) {
                hql.append(" and t.industryType like :industryType ");
                map.put("industryType", legalCasesQueryDTO.getIndustryType());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, legalCasesQueryDTO.getCurrentPage(), legalCasesQueryDTO.getPageSize());
    }

    public PageModel<LegalCases> queryLegalCasesPage(LegalCaseFontQueryDTO legalCasesQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from LegalCases t where 1=1");
        if (legalCasesQueryDTO != null) {

            if (StringUtils.isNotBlank(legalCasesQueryDTO.getMediaType())) {
                hql.append(" and t.mediaType = :mediaType ");
                map.put("mediaType", legalCasesQueryDTO.getMediaType());
            }

            if (StringUtils.isNotBlank(legalCasesQueryDTO.getId())) {
                hql.append(" and t.id = :id ");
                map.put("id", legalCasesQueryDTO.getId());
            }

            if (StringUtils.isNotBlank(legalCasesQueryDTO.getIndustryType())) {
                hql.append(" or t.industryType = :industryType ");
                map.put("industryType", legalCasesQueryDTO.getIndustryType());
            }

        }
        return this.queryForPageWithParams(hql.toString(), map, legalCasesQueryDTO.getCurrentPage() + 1, legalCasesQueryDTO.getPageSize());
    }

    public List<LegalCases> queryLegalCasesList(LegalCasesQueryDTO legalCasesQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from LegalCases t where 1=1 ");
        if (legalCasesQueryDTO != null) {
            if (StringUtils.isNotBlank(legalCasesQueryDTO.getName())) {
                hql.append(" and t.name like :name ");
                map.put("name", "%" + legalCasesQueryDTO.getName() + "%");
            }
        }
        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public int updateSelectiveById(LegalCases legalCases) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update LegalCases t set");
        if (legalCases != null) {

            if (!StringUtils.isNotBlank(legalCases.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(legalCases.getName())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.name = :name ");
                } else {
                    hql.append(" t.name = :name ");
                }
                map.put("name", legalCases.getName());
            }

            if (StringUtils.isNotBlank(legalCases.getIndustryType())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.industryType = :industryType ");
                } else {
                    hql.append(" t.industryType = :industryType ");
                }
                map.put("industryType", legalCases.getIndustryType());
            }
            if (StringUtils.isNotBlank(legalCases.getCoverImg())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.coverImg = :coverImg ");
                } else {
                    hql.append(" t.coverImg = :coverImg ");
                }
                map.put("coverImg", legalCases.getCoverImg());
            }


            if (StringUtils.isNotBlank(legalCases.getMediaType())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.mediaType = :mediaType ");
                } else {
                    hql.append(" t.mediaType = :mediaType ");
                }
                map.put("mediaType", legalCases.getMediaType());
            }

            if (StringUtils.isNotBlank(legalCases.getDescription())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.description = :description ");
                } else {
                    hql.append(" t.description = :description ");
                }
                map.put("description", legalCases.getDescription());
            }

            if (StringUtils.isNotBlank(legalCases.getAnalysis())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.analysis = :analysis ");
                } else {
                    hql.append(" t.analysis = :analysis ");
                }
                map.put("analysis", legalCases.getAnalysis());
            }
            hql.append("where t.id=:id ");
            map.put("id", legalCases.getId());
        }
        return this.update(hql.toString(), map);
    }
}